home *** CD-ROM | disk | FTP | other *** search
/ Network Supervisor's Toolkit / Network Supervisor's Toolkit.iso / tools / nwtp06 / fget.pas < prev    next >
Pascal/Delphi Source File  |  1996-07-10  |  6KB  |  201 lines

  1. {$X+,V-,B-,I-}
  2. program Fget; { Listening Process / receiver / Slave }
  3.  
  4. { Testprogram for the nwPEP unit / NwTP 0.6 API. (c) 1993,1995, R.Spronk }
  5.  
  6. {$DEFINE noTRACE}
  7.  
  8. uses crt,nwMisc,nwIPX,nwPEP;
  9.  
  10. Var ListenECB     :Tecb;       { used to listen for packets }
  11.     ListenPepHdr  :TpepHeader;
  12.  
  13.     SendECB       :Tecb;       { used to send acknowledgements }
  14.     SendPepHdr    :TpepHeader;
  15.  
  16.     IOsocket      :word;
  17.     DataBuffer    :array[1..546] of byte;
  18.     SendDataBuffer:byte;
  19.  
  20.     PacketReceived   :Boolean;
  21.     LastTransactionID:LongInt;
  22.  
  23.     NewStack:array[1..8192] of word;  { !! used by ESR }
  24.     StackBottom:word;                 { !! used by ESR }
  25.  
  26. Procedure CheckError(err:boolean; errNbr:word);
  27. begin
  28. if err
  29.  then begin
  30.       CASE errNbr of
  31.        $0100:writeln('IPX needs to be installed.');
  32.        $0101:writeln('ERROR: Connection not established. A Timeout occured');
  33.        $0102:writeln('ERROR: The transfer is aborted; A timeout occured.');
  34.        $0108:writeln('Transfer aborted.');
  35.        $0300:writeln('The supplied path doesn'' exist / no write rights in directory.');
  36.        $0301:writeln('Error writing to file / no write rights in directory.');
  37.        $10FE:writeln('Error opening socket: Socket Table Is Full.');
  38.        $10FF:writeln('Error opening socket: Socket is already open.');
  39.        else writeln('Unspecified error.');
  40.       end; {case}
  41.       IPXcloseSocket(IOsocket);
  42.       halt(1);
  43.       end;
  44. end;
  45.  
  46. Function TimeOut(t1,t2:word;n:byte):boolean;
  47. { ticks t2 - ticks t1 > n seconds ? }
  48. Var lt1,lt2:LongInt;
  49. begin
  50. lt2:=t2;
  51. if t1>t2 then lt2:=lt2+$FFFF;
  52. TimeOut:=(lt2-t1)>(n*18);
  53. end;
  54.  
  55. {$F+}
  56. Procedure ListenAndAckHandler;
  57. begin
  58. If (ListenECB.CompletionCode<>0)
  59.  or (ListenPepHdr.IPXhdr.packetType<>PEP_PACKET_TYPE)
  60.  or (ListenPepHdr.clienttype<>$EA)
  61.  or (ListenPepHdr.TransactionID<LastTransactionID) { discard dupe old packet }
  62.   then IPXlistenForPacket(ListenECB)
  63.   else begin
  64.        PacketReceived:=(ListenPepHdr.transactionID>LastTransactionID); { new packet received }
  65.  
  66.        { Acknowledge new packets and duplicates of the latest packet, }
  67.        { as the original acknowledgement may have been lost. }
  68.        LastTransactionID:=ListenPepHdr.TransactionID;
  69.  
  70.        { Setup acknowledgement ECB and PEPheader, and send it. }
  71.        if SendECB.InUseFlag=0
  72.         then begin
  73.              ListenPepHdr.IPXhdr.source.socket:=swap(ListenPepHdr.IPXhdr.source.socket);
  74.              { socket is hi-lo in IPX/PEPHeaders. SetupSendECB expects lo-hi }
  75.              PEPsetupSendECB(NIL,IOsocket,ListenPepHdr.IPXhdr.source,
  76.                              @SendDataBuffer,0,
  77.                              SendPepHdr,SendECB);
  78.              SendPepHdr.TransactionId:=LastTransactionID;
  79.              SendPepHdr.ClientType:=$EA;
  80.              IPXsendPacket(SendECB);
  81.              end;
  82.        end;
  83. end;
  84. {$F-}
  85.  
  86. {$F+}
  87. Procedure ListenAndAckESR; assembler;
  88. asm
  89.     mov dx, seg stackbottom
  90.     mov ds, dx
  91.  
  92.     mov dx,ss  { setup of a new local stack }
  93.     mov bx,sp  { ss:sp copied to dx:bx}
  94.     mov ax,ds
  95.     mov ss,ax
  96.     mov sp,offset stackbottom
  97.     push dx    { push old ss:sp on new stack }
  98.     push bx
  99.     CALL ListenAndAckHandler
  100.     pop bx     { restore ss:sp from new stack }
  101.     pop dx
  102.     mov sp,bx
  103.     mov ss,dx
  104. end;
  105. {$F-}
  106.  
  107. Var ticks,ticks2 :word;
  108.     FileName:string;
  109.     FileSize:LongInt;
  110.     DirName:string;
  111.     f:file;
  112.     BytesToWrite,BytesWritten:word;
  113.  
  114. begin
  115. IpxInitialize;
  116. CheckError(nwIPX.result>0,$100);
  117.  
  118. If (pos('?',ParamStr(1))>0) or (paramcount>1)
  119.  then begin
  120.       writeln('Usage: FGET <directory>');
  121.       writeln('-The File sent by FSEND on another workstation');
  122.       writeln('will be copied to the supplied directory.');
  123.       halt(1);
  124.       end;
  125. If paramcount=1
  126.  then DirName:=ParamStr(1)
  127.  else DirName:='.';
  128.  
  129. IF NOT (DirName[ord(dirName[0])] IN [':','\'])
  130.  then DirName:=DirName+'\';
  131. assign(f,DirName+'temp.$$$');
  132. rewrite(f,1);
  133. CheckError(IOresult<>0,$0300);
  134. close(f);
  135.  
  136. IOSocket:=$5678;
  137. IPXopenSocket(IOsocket,SHORT_LIVED_SOCKET);
  138. CheckError(nwIPX.result>0,$1000+nwIPX.result);
  139.  
  140. { Setup of ECB and PepHeader, start listening for incoming packets. }
  141. LastTransactionID:=0;
  142. PacketReceived:=False;
  143. PEPSetupListenECB(Addr(ListenAndAckESR),IOsocket,@DataBuffer,546,
  144.                   ListenPepHdr,ListenECB);
  145. IPXListenForPacket(ListenECB);
  146. writeln('Waiting for FSEND to start sending.. (any key to abort)');
  147.  
  148. IPXGetIntervalMarker(ticks);
  149. REPEAT
  150.  IPXrelinquishControl;
  151.  IPXGetIntervalMarker(ticks2);
  152.  CheckError(TimeOut(ticks,ticks2,130),$101);{ error if a timeout occurred }
  153.  CheckError(Keypressed,$108);
  154. UNTIL PacketReceived;
  155.  
  156. writeln('Handshaking.. Initiating transfer process.');
  157. {$IFDEF TRACE}
  158. writeln('Received PacketID:',LastTransactionID);
  159. {$ENDIF}
  160.  
  161. { do something with DataBuffer: the data that was just received. }
  162. { the first packet contains the filename and filesize }
  163. Move(DataBuffer[1],FileName[0],15);
  164. Move(DataBuffer[16],FileSize,4);
  165. writeln('Receiving file ',FileName,', size: ',FileSize);
  166.  
  167. assign(f,DirName+filename);
  168. rewrite(f,1);
  169. BytesToWrite:=512;
  170.  
  171. REPEAT { Listen for remaining packets  }
  172.  Packetreceived:=false;
  173.  
  174.  While SendECB.InuseFlag<>0
  175.   do IPXrelinquishControl;
  176.  
  177.  IPXListenForPacket(ListenECB);
  178.  IPXGetIntervalMarker(ticks);
  179.  Repeat
  180.   IPXrelinquishControl;
  181.   IPXGetIntervalMarker(ticks2);
  182.   CheckError(TimeOut(ticks,ticks2,10),$102); { error if Timeout occurred }
  183.   CheckError(Keypressed,$108);
  184.  until PacketReceived;
  185.  {$IFDEF TRACE}
  186.  writeln('Received packet#:',LastTransactionID);
  187.  {$ENDIF}
  188.  
  189.  { Write DataBuffer to disk. }
  190.  IF FileSize<512
  191.   then BytesToWrite:=FileSize;
  192.  BlockWrite(f,DataBuffer,BytesToWrite,BytesWritten);
  193.  CheckError(BytesToWrite<>BytesWritten,$0301);
  194.  
  195.  FileSize:=FileSize-512;
  196. UNTIL (FileSize<=0); { entire file received }
  197.  
  198. writeln('Transfer complete.');
  199. IPXcloseSocket(IOsocket);
  200. close(f);
  201. end.